﻿using System;
using System.Net;

namespace CS.Beehive
{
    /// <summary>
    /// 抽象的登录信息
    /// </summary>
    public abstract class Session
    {
        protected Session()
        {
            SessionId = Guid.NewGuid().ToString();
            CreateTime = DateTime.Now;
        }

        /// <summary>
        /// 客户端Id
        /// </summary>
        public long ClientId { get; protected set; }
        /// <summary>
        /// 全局唯一标识，只读
        /// </summary>
        public string SessionId { get; private set; }
        /// <summary>
        /// 用户Id
        /// </summary>
        public int UserId { get; set; }
        /// <summary>
        /// 角色Id
        /// </summary>
        public int RoleId { get; set; }
        /// <summary>
        /// 组Id
        /// </summary>
        public int GroupId { get; set; }
        /// <summary>
        /// 用户名
        /// </summary>
        public string Username { get; set; }
        /// <summary>
        /// 授权Token
        /// </summary>
        public string Token { get; set; }
        /// <summary>
        /// 是否经过授权（初步简单判断）
        /// </summary>
        public bool IsAuth { get; set; }
        /// <summary>
        /// 用户友好名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; private set; }
        /// <summary>
        /// 最后活动时间(获取系统启动后运行到当前的时间，毫秒：Environment.TickCount)
        /// </summary>
        public int ActiveTickCount { get; internal set; }
        /// <summary>
        /// 空闲时间，毫秒
        /// </summary>
        public int IdleTickCount
        {
            get { return Math.Abs(Environment.TickCount - ActiveTickCount); }
        }
        /// <summary>
        /// 用户相关的附加信息
        /// </summary>
        public object Tag { get; set; }

        /// <summary>
        /// 客户端终结点
        /// </summary>
        public IPEndPoint EndPoint { get; protected set; }

        /// <summary>
        /// 清除Session
        /// </summary>
        public virtual void Abandon()
        {
            ClientId = 0;
            UserId = 0;
            RoleId = 0;
            GroupId = 0;
            Username = string.Empty;
            Token = string.Empty;
            IsAuth = false;
            Name = string.Empty;
            ActiveTickCount = 0;
            Tag = null;
        }
    }
}